home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cocktail
/
r2l.lha
/
r2l
/
src
/
r2l.lalr
< prev
next >
Wrap
Text File
|
1992-08-18
|
7KB
|
280 lines
(* $Id: rex.lalr,v 1.6 90/06/11 11:28:10 grosch rel Locker: grosch $ *)
(* $Log: rex.lalr,v $
*)
(* Ich, Doktor Josef Grosch, Informatiker, Nov. 1987 *)
GLOBAL {
FROM SYSTEM IMPORT ADR;
FROM Strings IMPORT tString, ArrayToString;
FROM StringMem IMPORT WriteString;
FROM Idents IMPORT tIdent, MakeIdent, WriteIdent, GetString;
FROM Texts IMPORT tText, WriteText;
FROM IO IMPORT StdOutput;
FROM StdIO IMPORT WriteC, WriteS, WriteI, WriteNl;
FROM Errors IMPORT ErrorMessageI;
FROM DefTable IMPORT NoDef, DefRange, tKind, GetKind, MakeIdentDef, GetDef;
CONST
IdentUndefined = 10 ;
ImproperUse = 11 ;
IdentDefBefore = 12 ;
Error = 3 ;
String = 7 ;
TYPE
tParsAttribute = RECORD Scan: Scanner.tScanAttribute; END;
VAR
string : tString;
ANY : tIdent;
Definition : DefRange;
complement : BOOLEAN;
Export, Global, Local, Begin, Close, Default, Eof: tText;
PROCEDURE WriteChar (Ch: CHAR; InClass: BOOLEAN);
BEGIN
IF ('A' <= Ch) AND ( Ch <= 'Z') OR
('a' <= Ch) AND ( Ch <= 'z') OR
('0' <= Ch) AND ( Ch <= '9') THEN
WriteC (Ch);
ELSE
CASE Ch OF
| 012C: WriteS ("\n");
| 011C: WriteS ("\t");
| 013C: WriteS ("\v");
| 010C: WriteS ("\b");
| 015C: WriteS ("\r");
| 014C: WriteS ("\f");
| '"' : WriteS ('\"');
| ']' : WriteS ("\]");
| '.' : WriteS ("\.");
| '^' : WriteS ("\^");
| '-' : WriteS ("\-");
| '\' : WriteS ("\\");
ELSE
IF InClass THEN
WriteC (Ch);
ELSE
WriteC ('"');
WriteC (Ch);
WriteC ('"');
END;
END;
END;
END WriteChar;
}
BEGIN { ArrayToString ("ANY", string); ANY := MakeIdent (string); }
TOKEN
Ident = 1
Number = 2
String = 3
Char = 4
TargetCode = 5
'EXPORT' = 32
'GLOBAL' = 6
'LOCAL' = 31
'BEGIN' = 7
'CLOSE' = 8
EOF = 34
DEFAULT = 36
DEFINE = 9
START = 10
RULES = 11
NOT = 30
'.' = 12
',' = 13
'=' = 14
':' = 15
':-' = 35
'#' = 33
'/' = 16
'|' = 17
'+' = 18
'-' = 19
'*' = 20
'?' = 21
'(' = 22
')' = 23
'[' = 24
']' = 25
'{' = 26
'}' = 27
'<' = 28
'>' = 29
OPER
LEFT COMPL
LEFT '|'
LEFT SEQUENCE
LEFT '+' '*' '?' '[' '-'
LEFT '(' '{'
LEFT Ident Char String
RULE
input : code
{ WriteS ("%{"); WriteNl;
WriteText (StdOutput, Export);
WriteText (StdOutput, Global);
WriteS ("%}"); WriteNl;
WriteNl;
}
define start rules
{ WriteNl; WriteS ("%%"); WriteNl;
WriteNl;
WriteS ("void BeginScanner ()"); WriteNl;
WriteS ("{"); WriteNl;
WriteText (StdOutput, Begin);
WriteS ("}"); WriteNl;
WriteNl;
WriteS ("void CloseScanner ()"); WriteNl;
WriteS ("{"); WriteNl;
WriteText (StdOutput, Close);
WriteS ("}"); WriteNl;
}
.
code :
| code 'EXPORT' TargetCode { Export := $3.Scan.Text; }
| code 'GLOBAL' TargetCode { Global := $3.Scan.Text; }
| code 'LOCAL' TargetCode { Local := $3.Scan.Text; }
| code 'BEGIN' TargetCode { Begin := $3.Scan.Text; }
| code 'CLOSE' TargetCode { Close := $3.Scan.Text; }
| code DEFAULT TargetCode { Default := $3.Scan.Text; }
| code EOF TargetCode { Eof := $3.Scan.Text;
WriteS ("/*** sorry - cannot translate EOF ***/"); WriteNl; }
.
define :
| DEFINE definitionList
.
start :
| START { WriteNl; WriteS ("%Start"); } identListDef { WriteNl; WriteNl; }
.
rules : RULES
{ WriteS ("%%"); WriteNl;
WriteNl;
WriteS ("%{"); WriteNl;
WriteText (StdOutput, Local);
WriteS ("%}"); WriteNl;
WriteNl;
}
ruleList
{ WriteS ('" " ;'); WriteNl;
WriteS ("\t ;"); WriteNl;
WriteS ("\n ;"); WriteNl;
WriteS (". {");
WriteText (StdOutput, Default);
WriteS ("}");
WriteNl;
}
.
definitionList :
| definitionList definition
.
ruleList :
| ruleList rule
.
identListDef : IdentDef
| identListDef IdentDef
| identListDef ',' IdentDef
.
IdentDef : Ident
{ WriteC (" "); WriteIdent (StdOutput, $1.Scan.Ident); }
.
identListUse : IdentUse
| identListUse { WriteC (","); } IdentUse
| identListUse ',' { WriteC (","); } IdentUse
.
IdentUse : Ident
{ WriteIdent (StdOutput, $1.Scan.Ident); }
.
definition : Ident '='
{ IF GetDef ($-1.Scan.Ident) = NoDef THEN
MakeIdentDef ($-1.Scan.Ident);
ELSE
GetString ($-1.Scan.Ident, string);
ErrorMessageI (IdentDefBefore, Error, $-1.Scan.Position,
String, ADR (string));
END;
WriteIdent (StdOutput, $-1.Scan.Ident); WriteC (" ");
}
regExpr '.' { WriteNl; }
.
rule : patternList ':' { WriteS (" {"); } TargetCode
{ WriteText (StdOutput, $4.Scan.Text); WriteC ("}"); WriteNl; }
| patternList ':-' { WriteS (" {"); } TargetCode
{ WriteText (StdOutput, $4.Scan.Text); WriteC ("}"); WriteNl; }
.
patternList : pattern
| patternList ',' { WriteS (" |"); } pattern
.
pattern : startStates leftJust regExpr rightContext rightJust
.
startStates :
| '#' { WriteC ("<"); } identListUse '#' { WriteC (">"); }
| NOT '#' identListUse '#'
{ WriteS ("/*** sorry - cannot translate NOT ***/"); WriteNl; }
.
leftJust :
| '<' { WriteC ("^"); }
.
rightContext :
| '/' { WriteC ("/"); } regExpr
.
rightJust :
| '>' { WriteC ("$"); }
.
regExpr : regExpr '|' { WriteC ("|"); } regExpr
| regExpr regExpr PREC SEQUENCE
| regExpr '+' { WriteC ("+"); }
| regExpr '*' { WriteC ("*"); }
| regExpr '?' { WriteC ("?"); }
| regExpr '[' Number ']'
{ WriteC ("{"); WriteI ($3.Scan.Number, 0); WriteC (",");
WriteI ($3.Scan.Number, 0); WriteC ("}"); }
| regExpr '[' Number '-' Number ']'
{ WriteC ("{"); WriteI ($3.Scan.Number, 0); WriteC (",");
WriteI ($5.Scan.Number, 0); WriteC ("}"); }
| '(' { WriteC ("("); } regExpr ')' { WriteC (")"); }
| compl charSet
| Char { WriteChar ($1.Scan.Ch, FALSE); }
| Ident
{ Definition := GetDef ($1.Scan.Ident);
IF Definition # NoDef THEN
IF GetKind (Definition) = Ident THEN
WriteC ("{"); WriteIdent (StdOutput, $1.Scan.Ident); WriteC ("}");
ELSE
GetString ($1.Scan.Ident, string);
ErrorMessageI (ImproperUse, Error, $1.Scan.Position,
String, ADR (string));
END;
ELSE
IF $1.Scan.Ident = ANY THEN
WriteC (".");
ELSE
WriteIdent (StdOutput, $1.Scan.Ident);
END;
END;
}
| String
{ WriteC ('"'); WriteString (StdOutput, $1.Scan.String); WriteC ('"'); }
.
compl : { complement := FALSE; } PREC COMPL .
charSet : '-' { complement := NOT complement; } charSet
| '{' { WriteC ("["); IF complement THEN WriteC ("^"); END; }
rangeList '}' { WriteC ("]"); }
.
rangeList :
| rangeList range
.
range : Char { WriteChar ($1.Scan.Ch, TRUE); }
| Char '-' { WriteChar ($-1.Scan.Ch, TRUE); WriteC ("-"); }
Char { WriteChar ($4.Scan.Ch, TRUE); }
.